package com.sc.base.admin.modules.sys.controller;

import com.sc.base.admin.common.utils.PageQuery;
import com.sc.base.admin.common.utils.PageUtils;
import com.sc.base.admin.common.utils.R;
import com.sc.base.admin.modules.comm.controller.AbstractController;
import com.sc.base.admin.modules.sys.entity.SystTaskEntity;
import com.sc.base.admin.modules.sys.service.SysTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by lw on 2018/10/15.
 */

@RestController
@RequestMapping("/sys/task")
public class SysTaskController extends AbstractController {
    @Autowired
    SysTaskService sysTaskService;



    //查询定时任务列表
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageQuery pageQuery = new PageQuery(params);
        List<SystTaskEntity> taskList =  sysTaskService.queryList(params);
        PageUtils pageUtil = new PageUtils(taskList, taskList.size(), 30, 0);
        return R.ok().put("page",pageUtil);
    }


    @RequestMapping("/queryByTaskId")
    public R queryByTaskId(@RequestParam Map<String, Object> params){
        SystTaskEntity systTaskEntity =  sysTaskService.queryTaskByOne(params);
        if(null!=systTaskEntity ){
            return R.ok().put("taskConfig",systTaskEntity);
        }
        return R.error("没有此ID数据！");
    }

    @RequestMapping("/update")
    @RequiresPermissions("sys:task:update")
    public R update(@RequestParam Map<String, Object> params){

        if(null!=params.get("id") && !"".equals(params.get("id"))){
            params.put("updateUser",getUserId());
            sysTaskService.updateTask(params);
        }else{
            return R.error("修改ID必填");
        }
        return R.ok();
    }


    @RequestMapping("/save")
    @RequiresPermissions("sys:task:add")
    public R save(SystTaskEntity systTaskEntity){
        systTaskEntity.setCreateUsre(String.valueOf(getUserId()));
        systTaskEntity.setUpdateUser(String.valueOf(getUserId()));
        sysTaskService.saveTask(systTaskEntity);
        return R.ok();
    }


    @RequestMapping("/del")
    @RequiresPermissions("sys:task:del")
    public R save(@RequestParam Map<String, Object> params){
        sysTaskService.delete(params);
        return R.ok();
    }

    @RequestMapping("/sendTask")
    public R sendTask(String taskType){
        if("sp_calc".equals(taskType)){
            logger.info("手动触发结算任务，请确认当月表数据已清理");
        }

        return R.ok();
    }


}
